package com.angola.sms

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.angola.sms.databinding.ActivityAppUpdateBinding
import com.angola.sms.update.ZHIVersionClient
import com.angola.sms.update.ZHIVersionIMPL


@SuppressLint("CustomSplashScreen")
class AppUpdateActivity : AppCompatActivity() {

    private lateinit var _binding: ActivityAppUpdateBinding

    private val RC_INSTALL_PCK = 10086

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        _binding = ActivityAppUpdateBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        try {
            if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
                finish()
                return
            }

            _binding.btnInstall.setOnClickListener {
                ZHIVersionIMPL.startInstallApk(ZHIVersionIMPL.getApkSavePath())
            }

            _binding.btnCancel.setOnClickListener {
                finish()
            }

            startCheckPermissions()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == RC_INSTALL_PCK) {
            startCheckPermissions()
        }
    }

    private fun startCheckPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (packageManager.canRequestPackageInstalls()) {
                startFileDownLoad()
            } else {
                getUnknownAppInstallPage()
            }
        } else {
            startFileDownLoad()
        }
    }

    private fun getUnknownAppInstallPage() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
                intent.data = Uri.parse("package:" + "com.angola.sms.inno")
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivityForResult(intent, RC_INSTALL_PCK)
            } else {
                startFileDownLoad()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("SetTextI18n")
    fun startFileDownLoad() {
        ZHIVersionClient.startDownload(ZHIVersionIMPL.productLink) { currentBytes, contentLength, done ->
            val progress = (currentBytes * 100 / contentLength).toInt()
            _binding.pbProgress.progress = progress
            _binding.tvProgress.text = "$progress%"
            _binding.tvProgress.visibility = View.VISIBLE

            _binding.btnInstall.visibility =
                if (done) View.VISIBLE else View.GONE
            _binding.btnCancel.visibility =
                if (done) View.VISIBLE else View.GONE

            if (done) {
                ZHIVersionIMPL.startInstallApk(ZHIVersionIMPL.getApkSavePath())
            }
        }
    }
}